require('es6-promise').polyfill();

import axios from 'axios'





export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

function loadUnreplyCount(data) {
  return {
    type: 'LOAD_UNREPLY_SUGGESTION_COUNT',
    data
  }
}


function loadSuggestionData(data) {
  return {
    type: 'LOAD_SUGGESTION_DATA',
    data
  }
}

export function loadSuggestionDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/suggestions',data).then(
      resp => dispatch(loadSuggestionData(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

//查询回复
function querySuggestionData(data) {
  return {
    type: 'QUERY_SUGGESTION_DATA',
    data
  }
}

export function querySuggestionDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/suggestions/queryReply',data)
    .then((resp)=>{
      dispatch(querySuggestionData(resp.data))
    })
    .catch((error)=> {
      dispatch(apiMsgAction(error.data.errMsg))
    });
  };
}


// 提交回复

export function replySuggestionDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/suggestions/reply',data)
    .then((resp)=>{
      dispatch(loadSuggestionDataAsync(data))
      dispatch(queryUnreplyCountAsync({}))
    })
    .catch((error)=> {
      dispatch(apiMsgAction(error.data.errMsg))
    })
  }
}

export function replyUpdateDataAsync(data) {
  return function(dispatch) {
    return axios.post('/api/suggestions/updateReply',data)
    .then((resp)=>{
      dispatch(querySuggestionDataAsync(data))
    })
    .catch((error)=> {
      dispatch(apiMsgAction(error.data.errMsg))
    })
  }
}

export function queryUnreplyCountAsync(data) {
  return function(dispatch) {
    return axios.post('/api/suggestions/queryUnreplyCount',data)
    .then((resp)=>{
      dispatch(loadUnreplyCount(resp.data))
    })
    .catch((error)=> {

    })
  }
}
